<template>
  <div>
    <el-card>
      <!-- 第一部分 -->
      <moreInput
        :base-que-obj="baseQueObj"
        @clearForm="clearForm"
        @checkFn="getBaseQueBankList"
      />

      <!-- 第二部分 -->
      <div style="margin-top: 10px">
        <el-alert
          :title="`共${total}条记录`"
          type="info"
          show-icon
          :closable="false"
        />
      </div>
      <!-- 第三部分 -->
      <div style="margin-top: 10px">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="number" label="试题编号" />
          <el-table-column prop="subject" label="学科" />
          <el-table-column prop="catalog" label="目录" />
          <el-table-column label="题干">
            <template slot-scope="scope">
              <span v-html="scope.row.question" />
            </template>
          </el-table-column>
          <el-table-column prop="addDate" label="录入时间">
            <template slot-scope="{ row }">
              {{ formatTime(row.addDate, 'YYYY-MM-DD') }}
            </template>
          </el-table-column>
          <el-table-column prop="difficulty" label="难度">
            <template slot-scope="{ row }">
              {{
                row.difficulty === '1'
                  ? '简单'
                  : row.difficulty === '2'
                  ? '一般'
                  : '困难'
              }}
            </template>
          </el-table-column>
          <el-table-column prop="creator" label="录入人" />
          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <el-button
                icon="el-icon-view"
                type="primary"
                style="
                  color: #409eff;
                  background: #ecf5ff;
                  border-color: #b3d8ff;
                "
                size="mini"
                circle
                @click="previewFn(row)"
              />
              <el-button
                icon="el-icon-edit"
                type="success"
                style="
                  color: #67c23a;
                  background: #f0f9eb;
                  border-color: #c2e7b0;
                "
                size="mini"
                circle
                @click="handleEdit(row)"
              />
              <el-button
                icon="el-icon-delete"
                type="danger"
                style="
                  color: #f56c6c;
                  background: #fef0f0;
                  border-color: #fbc4c4;
                "
                size="mini"
                circle
                @click="handleDelete(row)"
              />
              <el-button
                icon="el-icon-check"
                type="warning"
                style="
                  color: #e6a23c;
                  background: #fdf6ec;
                  border-color: #f5dab1;
                "
                size="mini"
                circle
                @click="intoSelected(row)"
              />
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          :current-page="page"
          :page-sizes="[2, 5, 10, 15, 20]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 预览弹层 -->
    <previewDialog
      ref="perDialog"
      :dialog-form-visible.sync="dialogFormVisible"
    />
  </div>
</template>

<script>
import {
  deleteQueApi,
  getBaseQueBankListApi,
  intoSelectedApi
} from '@/api/queBank'
import previewDialog from './components/previewDialog.vue'
import moreInput from '@/components/moreInput/moreInput.vue'
import { formatTime } from '@/filter'
export default {
  components: {
    previewDialog,
    moreInput
  },
  data() {
    return {
      // 基础学科对象
      baseQueObj: {
        // 学科
        subjectID: '',
        // 二级目录
        catalogID: '',
        // 标签
        tags: '',
        // 关键字
        keyword: '',
        // 试题类型
        questionType: '',
        // 难度
        difficulty: '',
        // 方向
        direction: '',
        // 录入人
        creatorID: '',
        // 题目备注
        remarks: '',
        // 企业简称
        shortName: '',
        // 省份
        province: '',
        // 城市
        city: ''
      },
      // 表格数据列表
      tableData: [],
      // 当前页
      page: 1,
      // 每页的数据条数
      pagesize: 10,
      // 总数
      total: 0,

      // icon
      iconState: 'el-icon-check',
      // 预览弹层的显隐
      dialogFormVisible: false
    }
  },
  created() {
    this.getBaseQueBankList({
      page: this.page,
      pagesize: this.pagesize
    })
  },
  methods: {
    formatTime,
    // 改变每页的条数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.getBaseQueBankList({
        page: this.page,
        pagesize: this.pagesize
      })
    },
    // 改变当前的页码
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      this.getBaseQueBankList({
        page: this.page,
        pagesize: this.pagesize
      })
    },
    // 获取基础题库列表
    async getBaseQueBankList(val) {
      const res = await getBaseQueBankListApi(val)
      // res.data.items.forEach((item) => {
      // item.addDate = formatTime(item.addDate, 'YYYY-MM-DD')
      // if (item.difficulty === '1') {
      //   item.difficulty = '简单'
      // } else if (item.difficulty === '2') {
      //   item.difficulty = '一般'
      // } else {
      //   item.difficulty = '困难'
      // }
      // })
      this.tableData = res.data.items
      this.total = res.data.counts
    },
    // 清空搜索栏数据
    clearForm() {
      this.baseQueObj = {
        // 学科
        subjectID: '',
        // 二级目录
        catalogID: '',
        // 标签
        tags: '',
        // 关键字
        keyword: '',
        // 试题类型
        questionType: '',
        // 难度
        difficulty: '',
        // 方向
        direction: '',
        // 录入人
        creatorID: '',
        // 题目备注
        remarks: '',
        // 企业简称
        shortName: '',
        // 省份
        province: '',
        // 城市
        city: ''
      }
    },
    // 点击预览
    previewFn(row) {
      this.dialogFormVisible = true
      this.$refs.perDialog.getBaseQueBankInfo(row.id)
    },
    // 点击编辑
    handleEdit(row) {
      this.$router.push({
        path: '/questions/new',
        query: { id: row.id }
      })
    },
    // 点击加入精选
    async intoSelected(row) {
      await intoSelectedApi(row.id, 1)
      this.$message.success('加入成功')
    },
    // 点击删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await deleteQueApi(row.id)
          this.getBaseQueBankList({
            page: this.page,
            pagesize: this.pagesize
          })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped lang="scss">
.el-input {
  width: 100%;
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #ffffff;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
